<?php

class imageInspector
{
    protected $image;

    function __construct($image)
    {
        if (! is_resource($image)) {
            throw new Exception();
        }

        $this->image = $image;
    }

    protected function getAvgColor($X, $Y)
    {
        $dx = 5;
        $dy = 5;

        $red = 0;
        $green = 0;
        $blue = 0;

        $count = 0;
        for ($x = 0; $x < 2*$dx; $x++) {
            for ($y = 0; $y < 2*$dy; $y++) {
                $color = imagecolorsforindex($this->image, imagecolorat($this->image, $X - $dx + $x, $Y - $dy + $y));
                if (isset($color["red"]) && isset($color["green"]) && isset($color["blue"])) {
                    $count++;
                    $red += $color["red"];
                    $green += $color["green"];
                    $blue += $color["blue"];
                }
            }
        }
        if (! $count) {
            return null;
        }
        $red /= $count;
        $green /= $count;
        $blue /= $count;

        return array(
            "red" => $red,
            "green" => $green,
            "blue" => $blue,
        );
    }

    protected function getMaxiColor($X, $Y)
    {
        $color = $this->getAvgColor($X, $Y);
        if (isset($color)) {
            asort($color);
            end($color);
            return key($color);
        }

        return null;
    }

    function getMatrix($startX, $startY, $offX, $offY, $cols, $rows)
    {
        $result = array();
        for ($col = 0; $col < $cols; $col++) {
            for ($row = 0; $row < $rows; $row++) {
                $result[$row][$col] = $this->getMaxiColor($startX + $offX * $col, $startY + $offY * $row);
            }
        }

        return $result;
    }


}

set_time_limit(0);
header("Content-Type: text/plain; charset=UTF-8");

$tmp_file = dirname(__FILE__).DIRECTORY_SEPARATOR."tmp.png";
$md5_check_sum_file = dirname(__FILE__).DIRECTORY_SEPARATOR."sum.txt";
$result_file = dirname(__FILE__).DIRECTORY_SEPARATOR."result.txt";

$file_content = file_get_contents("http://www.parter.ua/ua/himg2.html?event=1531");
file_put_contents($tmp_file, $file_content);

$new_sum = filesize($tmp_file).md5_file($tmp_file);
$old_sum = null;
if (file_exists($md5_check_sum_file)) {
    $old_sum = file_get_contents($md5_check_sum_file);
}
if (empty($old_sum) || ($old_sum !== $new_sum)) {

    /*file_put_contents($md5_check_sum_file, $new_sum);

    $inspector = new imageInspector(imagecreatefrompng($tmp_file));
    $result = $inspector->getMatrix(27, 328, 18, 18, 60, 10);

    $sum = array();
    $sum["Всего"] = 0;
    $sum["Свободно"] = 0;
    $sum["Занято"] = 0;
    $sum["Бронь"] = 0;
    foreach ($result as $cols) {
        foreach ($cols as $col) {
            switch ($col) {

            case "green":
                $sum["Свободно"]++;
                break;

            case "red":
                $sum["Занято"]++;
                break;

            case "blue":
                $sum["Бронь"]++;
                break;
            }
        }
    }
    $sum["Всего"] = $sum["Свободно"] + $sum["Занято"] + $sum["Бронь"];
    if ($sum["Свободно"]) {
        call_user_func_array("green", array($sum["Свободно"], "FUN"));
    }

    $result = array($sum, $result);

    file_put_contents($result_file, serialize($result));*/
}

//var_dump(unserialize(file_get_contents($result_file)));
echo plurar_mode_ru(1);
green(1, "asd");

function green($num, $zone)
{
    $nums = "";

    switch (plurar_mode_ru($num)) {

    case 0:
    case 1:
        $nums = "свободное место";
        break;

    case 2:
        $nums = "свободных места";
        break;

    case 3:
        $nums = "свободных мест";
        break;
    }

    $nums = $num." ".$nums;

    $sms = sprintf("Есть %s в зоне %s.", $nums, $zone);

    exit($sms);

    foreach (array("denis@philonov.com"/*, "380681261031@sms.beeline.ua"*/) as $email) {
        $from = "philonov@gmail.com";
        mb_internal_encoding("UTF-8");
        $subject = mb_encode_mimeheader("Есть билеты на концерт", "UTF-8", "B", "\r\n");
        $from_name = mb_encode_mimeheader("Филонов Денис", "UTF-8", "B", "\r\n");
        $to_name = mb_encode_mimeheader("Филонов Денис", "UTF-8", "B", "\r\n");

        $headers = "";
    	$headers .= "From: {$from_name} <{$from}>\r\n";
    	$headers .= "To: {$to_name} <{$email}>\r\n";
    	$headers .= "MIME-Version: 1.0\r\n";
    	$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
    	$headers .= "Content-Transfer-Encoding: base64\r\n";

        ini_set('SMTP', "alt1.aspmx.l.google.com");
        ini_set('smtp_port', 25);
        ini_set("sendmail_from", $from);
    	mail($email, $subject, chunk_split(base64_encode($sms)), $headers, ' -ba');
    }
}

function plurar_mode_ru($n)
{
    return ($n == 1)
        ? 0
        : (((($n % 10) == 1) && (($n % 100) != 11))
            ? 1
            : (((($n % 10) >= 2) && (($n % 10) <= 4) && ((($n % 100) < 10) || (($n % 100) > 20)))
                ? 2
                : 3
            )
        );
}

?>